package jzOffer;

public class Num39 {
    public int majorityElement(int[] nums) {
        if(nums.length==0) return 0;
        int result=nums[0];
        int times=1;;
        for(int i=1;i<nums.length;i++){
            if(times!=0){
                if(nums[i]==result) ++times;
                else --times;
            }else{
                result=nums[i];
                ++times;
            }
        }
        times=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]==result) times++;
        }
        return (times>nums.length/2)?result:0;
    }
}
